package com.itheima.dao;

import com.itheima.domain.Student;

import java.util.ArrayList;

/*
学生  数据操作类
数组

增
删
改
查
 */
public class StudentDao implements BaseStudentDao{
    //创建数组
    private static Student[] students = new Student[35];

    static {
        //数据的初始化工作
        Student student1 = new Student("0001", "坤坤1", 26, "1996");
        Student student2 = new Student("0002", "坤坤2", 26, "1996");
        Student student3 = new Student("0003", "坤坤3", 26, "1996");
        Student student4 = new Student("0004", "坤坤4", 26, "1996");

        students[0]=student1;
        students[1]=student2;
        students[2]=student3;
        students[3]=student4;
    }
    // 真正的向数组中添加数据
    public boolean addStudent(Student student) {
        //向数组中添加 元素
        //找空位 找到空位 放下即可
        for (int i = 0; i < students.length; i++) {
                Student stu= students[i];
                if(stu==null){
                    students[i]=student;
                    return true;
                }
        }

        //没有空位  返回失败
        return false;
    }

    public Student[] findAllStudents() {
        return students;

    }

    public void deleteStudent(String sid) {
        //找人
        int index = getIndex(sid);
        students[index]=null;
    }

    public void updateStudent(String sid, Student student) {
        //找人
        int index = getIndex(sid);
        students[index]=student;

    }

    //根据条件查
    public  int getIndex(String sid){
        int index=-1;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if(student!=null&& student.getSid().equals(sid)){
                index=i;
                break;
            }
        }
        return index;

    }
}
